layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func', 'upload'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var upload = layui.upload;
    var flag = true;
    var oldText;

    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };

    //监听单元格编辑
    table.on('edit(noticeTable)', function (obj) {
        oldText = $(this).prev().text();
        var iscwm=isNumber(obj.data.cwm);
        if(obj.data.cwm!=0&&CheckIsNullOrEmpty(obj.data.cwm)==true&&iscwm==true&&obj.data.cwm.charAt(0)!=0){
                data = obj.data //得到所在行所有键值
            $.ajax({
                type: "PUT",
                url: "/btyBrand/updateCwm",
                datatype: "json",
                data: data,
                success: function (data) {
                    if(data==401){
                        Feng.error("已存在该财务码！");
                        obj.update({
                            cwm: oldText,
                        });
                    }else {
                        Feng.success("修改成功！");
                        Notice.onMaxCwm();
                        flag = true;
                    }
                }, error: function () {
                    Feng.error("修改失败！");
                }
            });
        }else if(obj.data.cwm==0||CheckIsNullOrEmpty(obj.data.cwm)==false||obj.data.cwm.charAt(0)==0||iscwm==false){
            Feng.error("财务码格式不正确，请重新输入！");
            setTimeout(function(){
                obj.update({
                    cwm: oldText,
                });
            },0);
        }
    });

    function CheckIsNullOrEmpty(value) {
        //正则表达式用于判斷字符串是否全部由空格或换行符组成
        var reg = /^\s*$/
        //返回值为true表示不是空字符串
        return (value != null && value != undefined && !reg.test(value))
    }

    function isNumber(str){
        if(""==str){
            return false;
        }
        var reg = /\D/;
        return str.match(reg)==null;
    }

    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        Notice.onMaxCwm();
        return [[
            {field: 'id', align: "center", hide: true, sort: true, title: 'id'},
            {align: 'center', type: "numbers", title: '序号'},
            {field: 'dm', align: "left",title: '店名'},
            {field: 'kyrq', align: "left",title: '首次营业日'},
            {field: 'yaoma', align: "left", sort: false, title: '妖码'},
            {field: 'cwm', align: "left", sort: false, title: '财务码'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Notice.search = function (enumeration) {
        var queryData = {};
        if(enumeration==1){
            //初始页使用 条件查询 查询全部
            queryData['cwm'] = 0;
            var searchAll= $("#searchAll").val().replace(/\s*/g,"");
            queryData['searchAll'] = searchAll;
        }else {
            //查询
            queryData['cwm'] = '';
            var searchAll= $("#searchAll").val().replace(/\s*/g,"");
            queryData['searchAll'] = searchAll;
        }

        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    Notice.onMaxCwm = function () {
        var ajax = new $ax(Feng.ctxPath + "/btyBrand/maxCwm", function (data) {
            $('#maxCwm').text(data);
        });
        ajax.start();
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/btyBrand/listCwm',
        page: true,
        height: "full-59",
        cellMinWidth: 100,
        limit: 50,
        cols: Notice.initColumn(),
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.onMaxCwm();
        Notice.search();
        flag = true;
    });

    // 搜索按钮点击事件 未编码门店
    $('#btnSearChunencoded').click(function () {
        Notice.onMaxCwm();
        Notice.search(1);
        flag = true;
    });

    // 搜索按钮点击事件 同步营业日
    $('#btnSynchronous').click(function () {
        admin.btnLoading('#btnSynchronous', "同步营业日");
        Notice.onMaxCwm();
        setTimeout(function(){
            var ajax = new $ax(Feng.ctxPath + "/btyBrand/updateCwmAll", function (data) {
                Notice.search(1);
                flag = true;
                admin.btnLoading('#btnSynchronous', false);
                Feng.success("同步成功!");
                $('#btnSynchronous').removeAttr("disabled")
            }, function (data) {
                Feng.error("同步失败!" + data.responseJSON.message + "!");
                admin.btnLoading('#btnSynchronous', false);
                $('#btnSynchronous').removeAttr("disabled")
            });
            ajax.start();
        }, 1000);

    });

    // 搜索按钮点击事件 编辑财务码
    $('#btnEditCwm').click(function () {
        if (flag) {
            flag = false;
            $("table tr").find('td:last-child').data("edit", true).css("background-color","#02a7f0");
        } else {
            flag = true;
            $("table tr").find('td:last-child').data("edit", false).css("background-color","");

        }
    });
});
